package com.woniuxy.joylifeshop.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.ShopCommodity;
import com.woniuxy.joylifeshop.exception.MerchantCommodityException;
import com.woniuxy.joylifeshop.service.MerchantCommodityService;
import com.woniuxy.joylifeshop.vo.ViewObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class MerchantCommodityController {

    @Autowired
    MerchantCommodityService merchantCommodityService;
    
    /**
    * @Author SooYoo
    * @Description 修改商品对象
    * @Param 
    * @Return 
    **/
    @RequestMapping("/editCommodity")
    public ViewObject editCommodity(@RequestBody ShopCommodity shopCommodity){
        //System.out.println(shopCommodity);
        ViewObject viewObject = new ViewObject();

        try {
            merchantCommodityService.modifyCommodityByObject(shopCommodity);

            viewObject.setMsg("修改商品资料成功");
            viewObject.setCode(0);
            viewObject.setCount(1);
            viewObject.setData(null);

            return viewObject;

        } catch (Exception e) {
            e.printStackTrace();

            viewObject.setMsg("修改商品资料失败");
            viewObject.setCode(0);
            viewObject.setCount(1);
            viewObject.setData(null);

            return viewObject;
        }

    }

    /**
    * @Author SooYoo
    * @Description 删除一条商品数据
    * @Param 
    * @Return 
    **/
    @RequestMapping("/deleteOneCommodity")
    public ViewObject deleteOneCommodity(@RequestBody String information){
        //封装VO
        ViewObject viewObject = new ViewObject();
        try {
            //删除商品对象
            merchantCommodityService.removeOneCommodity(information);
            //配置vo
            viewObject.setData(null);
            viewObject.setCode(0);
            viewObject.setCount(1);
            viewObject.setMsg("删除成功");
            return viewObject;
        } catch (MerchantCommodityException e) {
            e.printStackTrace();
            viewObject.setData(null);
            viewObject.setCode(0);
            viewObject.setCount(1);
            viewObject.setMsg("删除失败");
            return viewObject;
        }
    }



    /**
    * @Author SooYoo
    * @Description 初始化商品的后台数据
    * @Param 
    * @Return 
    **/
    @RequestMapping("/initCommodityByCondition")
    public ViewObject initCommodityByCondition (@RequestBody HashMap<String,String> information){
        //从json中获取数据
        String jsonstr = information.get("jsonstr");
        ShopCommodity shopCommodity = JSON.parseObject(jsonstr, ShopCommodity.class);
        //封装VO
        ViewObject viewObject = new ViewObject();
        //分页
        PageHelper.startPage(Integer.parseInt(information.get("page")),Integer.parseInt(information.get("limit")));
        //找到需要的商品对象
        List<ShopCommodity> shopCommodities = merchantCommodityService.readMerchantCommodityByCondition(shopCommodity);
        //分页2
        PageInfo pageInfo = new PageInfo(shopCommodities);
        List<PageInfo> pageInfos = new ArrayList<>();
        pageInfos.add(pageInfo);
        //配置vo
        viewObject.setData(pageInfos);
        viewObject.setCode(0);
        viewObject.setCount(new Long(pageInfo.getTotal()).intValue());
        viewObject.setMsg("查询成功");
        return viewObject ;


    }





}
